<template>
  <div class="bind-enterprise">
    <layout-header />
    <div class="bind-enterprise-main">
      <div class="bind-enterprise-container" v-loading="loading">
        <h1 class="bind-enterprise__title">请选择您的企业</h1>
        <div class="bind-enterprise-header">
          <el-pagination
            v-model:current-page="query.pageNum"
            :page-size="query.pageSize"
            :page-sizes="[5, 10, 15, 20]"
            layout="sizes, prev, pager, next"
            :total="query.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
          <el-input
            placeholder="请搜索企业名称"
            class="bind-enterprise-search"
            maxlength="30"
            v-model="query.enterpriseName"
            @blur="() => !query.enterpriseName && !enterpriseList.length && handleSearch()"
          >
            <template #suffix>
              <el-button class="bind-enterprise-search__btn" @click="handleSearch">搜索</el-button>
            </template>
          </el-input>
        </div>
        <el-radio-group class="bind-enterprise-list" v-model="selEnterprise">
          <el-radio
            v-for="enterprise in enterpriseList"
            :key="enterprise.enterpriseId"
            class="bind-enterprise-item"
            :value="enterprise.enterpriseId"
          >
            <div class="bind-enterprise-item-main">
              <!-- <div class="bind-enterprise-logo" v-if="enterprise.icon">
                <svg-icon :icon-class="enterprise.icon" />
              </div> -->
              <p>{{ enterprise.enterpriseName }}</p>
            </div>
          </el-radio>
        </el-radio-group>
        <div class="bind-enterprise-footer">
          <router-link
            v-if="userStore.checkRole(Role_Enum.enterprise_boss) && !userStore.user?.enterpriseId"
            to="/create"
          >
            未找到您的企业，去登记
          </router-link>
          <el-button type="primary" class="bind-enterprise-next" @click="next">下一步</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import LayoutHeader from '@/components/Layout/header.vue'
import useUserStore from '@/stores/user'
import { Role_Enum, Role_Center_Host } from '@/common/enum'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import api from '@/http/index'
import { useRoute, useRouter } from 'vue-router'
const loading = ref(false)
const userStore = useUserStore()
const router = useRouter()
const route = useRoute()
const returnUrl = route.query?.returnUrl
const loginRole = route.query?.loginRole
const selEnterprise = ref()
const query = reactive({
  pageNum: 1,
  pageSize: 5,
  enterpriseName: '',
  totalPage: null,
  total: null,
})

const handleSizeChange = (v) => {
  query.pageSize = v
  query.pageNum = 1
  getEnterpriseList()
}
const handleCurrentChange = (v) => {
  query.pageNum = v
  getEnterpriseList()
}
const handleSearch = () => {
  getEnterpriseList()
}

let enterpriseList = ref([])
const getEnterpriseList = () => {
  loading.value = true
  api.enterprise
    .getList(query)
    .then((res) => {
      const list = res?.data?.list || []
      enterpriseList.value = list
      query.totalPage = res?.data?.totalPage
      query.total = res?.data?.total
    })
    .finally(() => {
      loading.value = false
    })
}
getEnterpriseList()

const next = () => {
  if (!selEnterprise.value) {
    ElMessage({ message: '请选择企业', type: 'error' })
    return
  }
  loading.value = true
  api.user
    .bindEnterprise({
      userId: userStore.user.userId,
      enterpriseId: selEnterprise.value,
    })
    .then((res) => {
      if (!res.data) return ElMessage.error('绑定企业失败')
      ElMessage.success('绑定企业成功')
      if (returnUrl) {
        location.href = returnUrl
        return
      }
      const ucenter = Role_Center_Host[loginRole]
      location.href = ucenter
    })
    .finally(() => {
      loading.value = false
    })
}
</script>

<style lang="scss">
.bind-enterprise {
  width: 100%;
  background-color: #f5f5f5;
  height: 100vh;
  overflow-y: scroll;
  &__title {
    padding: 20px;
    color: #262626;
  }
  &-main {
    width: 100%;
    min-height: calc(100vh - 76px);
    padding: 28px 0;
  }
  &-container {
    position: relative;
    width: 928px;
    margin: 0 auto;
    background-color: #fff;
    box-shadow:
      0 12px 32px 12px rgba(0, 0, 0, 0.03),
      0 9px 24px 0 rgba(0, 0, 0, 0.05),
      0 6px 16px -8px rgba(0, 0, 0, 0.08),
      0 2px 8px -4px rgba(32, 97, 230, 0.1);
  }
  &-header {
  }
  &-list {
    width: 100%;
    display: flex;
    flex-direction: column;
  }
  &-item {
    width: 100%;
    height: 126px;
    margin: 0;
    padding: 0 40px;
    border: 2px solid transparent;
    transition: all 0.3s;
    &:hover {
      border-color: var(--el-color-primary);
      box-shadow:
        0 5px 12px 4px rgba(0, 0, 0, 0.09),
        0 3px 6px 0 rgba(0, 0, 0, 0.12),
        0 1px 2px -2px rgba(0, 0, 0, 0.16);
    }
    &:hover .el-radio__inner {
      border-color: var(--el-color-primary);
    }
    &.is-checked {
      border-color: var(--el-color-primary);
      background-color: var(--el-color-primary-light-8);
    }
    &-main {
      display: flex;
      align-items: center;
      margin-left: 18px;
      > p {
        color: rgba(0, 0, 0, 0.85);
        font-size: 20px;
        font-weight: 500;
      }
    }
    &-icon {
      line-height: 44px;
      text-align: center;
      font-size: 24px;
      width: 44px;
      height: 44px;
      background-color: var(--el-color-primary-light-9);
      border-radius: 50%;
      margin-right: 26px;
    }
    &:last-child .el-radio__label {
      &::after {
        display: none;
      }
    }
    .el-radio__label {
      flex: 1;
      height: 100%;
      display: flex;
      align-items: center;
      position: relative;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        bottom: -2px;
        transform: translateX(-50%);
        width: 100%;
        height: 1px;
        background-color: rgba(0, 0, 0, 0.15);
      }
    }
    .el-radio__inner {
      width: 26px;
      height: 26px;
    }
  }
  &-footer {
    display: flex;
    align-items: center;
    position: sticky;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 12px 24px;
    height: 64px;
    background: #fff;
    a {
      white-space: nowrap;
      margin-right: 20px;
      font-size: 16px;
      text-decoration: none;
      color: #a3a1a1;
      &:hover {
        color: var(--el-color-primary);
      }
    }
  }
  &-next {
    width: 100%;
    height: 100%;
    font-size: 16px;
  }
  &-header {
    display: flex;
    position: sticky;
    justify-content: space-between;
    top: 0;
    left: 0;
    padding: 24px 14px;
    z-index: 2;
    background-color: #fff;
  }
  &-search {
    width: 280px;
    .el-input__wrapper {
      padding-right: 0;
    }
    .el-input__wrapper.is-focus &__btn {
      color: var(--el-color-primary) !important;
      background-color: var(--el-color-primary-light-9) !important;
    }
    &__btn {
      border: none;
      margin-right: 1px;
      &:hover {
        color: var(--el-color-primary) !important;
        background-color: var(--el-color-primary-light-9) !important;
      }
    }
  }
}
</style>
